package com.citcc.business.service;

import com.citcc.business.bean.CdzAppUser;
import com.citcc.business.mapper.CdzAppUserMapper;
import com.citcc.common.utils.SHA256Util;
import com.citcc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CdzAppUserService {

    private final CdzAppUserMapper cdzAppUserMapper;

    @Autowired
    public CdzAppUserService(CdzAppUserMapper cdzAppUserMapper) {
        this.cdzAppUserMapper = cdzAppUserMapper;
    }

    //用户注册
    public String userRegister(CdzAppUser cdzAppUser) {
        int count = cdzAppUserMapper.isRegister(cdzAppUser.getPhone());
        if (count > 0) {
            return "该手机号已注册";
        }
        String code = System.getProperty(cdzAppUser.getPhone());
        if (null != code && code.equals(cdzAppUser.getCode())) {
            cdzAppUser.setPassword(SHA256Util.encrypt(cdzAppUser.getPhone() + cdzAppUser.getPassword()));
            cdzAppUserMapper.insertSelective(cdzAppUser);
        } else {
            return "验证码错误";
        }
        return null;
    }

    //用户修改密码
    public String userReset(CdzAppUser cdzAppUser) {
        int count = cdzAppUserMapper.isRegister(cdzAppUser.getPhone());
        if (count > 0) {
            String code = System.getProperty(cdzAppUser.getPhone());
            if (null != code && code.equals(cdzAppUser.getCode())) {
                cdzAppUser.setPassword(SHA256Util.encrypt(cdzAppUser.getPhone() + cdzAppUser.getPassword()));
                cdzAppUserMapper.userReset(cdzAppUser);
            } else {
                return "验证码错误";
            }
        } else {
            return "该手机号尚未注册";
        }
        return null;
    }

    //用户登录
    public String userLogin(CdzAppUser cdzAppUser) {
        int count = cdzAppUserMapper.isRegister(cdzAppUser.getPhone());
        if (count > 0) {
            cdzAppUser.setPassword(SHA256Util.encrypt(cdzAppUser.getPhone() + cdzAppUser.getPassword()));
            count = cdzAppUserMapper.userLogin(cdzAppUser);
            if (count == 0) {
                return "密码错误";
            } else if (StringUtils.isNotEmpty(cdzAppUser.getDeviceId())) {
                cdzAppUserMapper.updateDeviceId(cdzAppUser);
            }
            return null;
        } else {
            return "该手机号尚未注册";
        }
    }

    //根据手机号查询用户
    public CdzAppUser getAppUserByPhone(String phone) {
        return cdzAppUserMapper.getAppUserByPhone(phone);
    }

    //修改用户所有信息
    public int update(CdzAppUser appUser) {
        return cdzAppUserMapper.updateByPrimaryKeySelective(appUser);
    }

    //根据ID查询用户
    public CdzAppUser selectByPrimaryKey(String id) {
        return cdzAppUserMapper.selectByPrimaryKey(id);
    }

    //假删除用户
    public int delete(String id) {
        return cdzAppUserMapper.deleteByPrimaryKey(id);
    }

    //查询所有客户
    public List<CdzAppUser> selectAllList(CdzAppUser appUser) {
        return cdzAppUserMapper.selectAllList(appUser);
    }

    // 根据微信openid、支付宝openid查询用户信息
    public CdzAppUser getAppUserByOpenId(String weChatOpenId, String alipayOpenId) {
        return cdzAppUserMapper.selectAppUserByOpenId(weChatOpenId, alipayOpenId);
    }
}